const Zan = require('../../dist/index');
const { Tab, extend } = require('../../dist/index');
var app = getApp();
Page(Object.assign({}, Zan.Dialog, Tab, {
  data: {
    goodInfo: {},
    gid: "",
    sellerid: "",
    couponInfo: {},
    picWidth: wx.getSystemInfoSync().windowWidth,
    content: "",
    maxLength: 0,
    page: 1,

    good_xq_pl: {
      list: [{
        id: 'xq',
        name: '宝贝详情',
      }
      // , {
      //   id: "pl",
      //   name: "宝贝评论",
      // }
      ],
      selectedId: 'xq'
    },
  },

  // 分享
  onShareAppMessage: function () {
    return {
      // title: "原价:" + this.data.goodInfo.price + "元,券后价" + this.data.goodInfo.coupon_price + "元,立省" + this.data.goodInfo.quan + "元" + this.data.goodInfo.title,
      title: this.data.goodInfo.title,
      path: '/pages/detail/detail?gid=' + this.data.gid + '&id=' + this.data.id + '&sellerid=' + this.data.sellerid + "&shop_type=" + this.data.shop_type,
      success: function (res) {
        // 转发成功
      },
      fail: function (res) {
        // 转发失败
      }
    }
  },
  onShow: function () {
  },
  onLoad: function (options) {
    //console.log(options.gid)
    this.setData({
      couponInfo: wx.getStorageSync('couponInfo'),
      id: options.id,
      gid: options.gid,
      // pic_url: options.pic_url,
      // shop_type: options.shop_type,
      //is_kouling: app.globalData.is_kouling,
    });
    this.getgoodinfo();
  },

  handleZanTabChange(e) {
    var componentId = e.componentId;
    var selectedId = e.selectedId;
    this.setData({
      [`${componentId}.selectedId`]: selectedId,
    });
    if (selectedId === "xq") {
      this.getgoodinfo();
    } else {
      this.getpinglun();
    }
  },
  //取商品详情
  getgoodinfo: function () {
    wx.showLoading({
      title: '加载中',
    })
    var that = this;
    console.log(that.data.id)

    //取商品详情
    wx.request({
      url: app.globalData.apiurl + '/Home/Item/getDetail',
      data: {
        "id": that.data.id,
      },
      success: function (res) {
        that.setData({
          goodInfo: res.data.data,
          sellerid: res.data.data.sellerId,
          quanid: that.getquanid(res.data.data.click_url),

        })
        wx.hideLoading()      
      }
    })
  },
  getquanid: function (u) {
    var quanid = u.substring(u.indexOf("activityId=") + 11, u.indexOf("&pid"));
    if (quanid == "https://ul") {
      quanid = u.substring(u.indexOf("edetail?e=") + 10, u.indexOf("&traceId"));
    }
    return quanid;
  },

  //取评论
  getpinglun: function () {
    wx.showLoading({
      title: '加载中',
    })
    var that = this;
    wx.request({
      url: app.globalData.httpsurl,
      data: {
        "action": "getpl",
        gid: that.data.gid,
        seller_id: that.data.sellerid,
        "page": that.data.page,
      },
      method: "GET",
      success: function (res) {
        //console.log(res.data)
        if (res.data.data != null && res.data.data.length > 0) {
          that.setData({ pinglun: res.data.data, isLoading: false, isLoadover: false })
          wx.hideLoading()
        } else {
          that.setData({ isLoadover: true })
          wx.hideLoading()
          wx.showToast({
            title: '暂无更多评论',
          })
        }

      }
    })
  },

  //图片点击事件
  imgYu: function (event) {
    var src = event.currentTarget.dataset.src;//获取data-src
    var imgList = event.currentTarget.dataset.list;//获取data-list
    //图片预览
    wx.previewImage({
      current: src, // 当前显示图片的http链接
      urls: imgList // 需要预览的图片http链接列表
    })
  },
  onReachBottom: function () {
    if (this.data.good_xq_pl.selectedId == "pl") {
      this.setData({
        isLoading: true,
        page: this.data.page + 1,
      })
      this.getpinglun()
    }
  },

  //create coupon
  getCoupon: function (options) {
    wx.showLoading({ title: "优惠券正在生成" })
    var that = this;
    var msg = "打开「手机掏宝」即可「领取优惠券」并购买" + that.data.goodInfo.taotoken + "!";
    wx.setClipboardData({
      data: msg,
      success: function (res) {
        wx.showLoading({ title: "优惠券生成成功" });
        setTimeout(function () {
          wx.hideLoading()
        }, 2000)
      },
    });
    
    that.showZanDialog({
      title: '优惠券领取成功',
      content: msg,
      buttons: [{
        text: '使用教程',
        type: 'explain'
      }, {
        text: '确定',
        color: '#3CC51F',
        type: 'confirm'
      }]
    }).then(({ type }) => {
      if (type === 'explain') {
        wx.navigateTo({
          url: "../../pages/explain/explain",
        })
      }
      //console.log('=== dialog with custom buttons ===', `type: ${type}`);
    });
  
  
  }
}))
